package JDK8;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*
    将Stream流转为集合
 */
public class StreamAPI1 {
    //将Stream流转换为List集合
    @Test
    public void test1(){
        ArrayList<String> list=new ArrayList<>();
        Collections.addAll(list,"张杰","张曼玉","张无忌","刘德华","黎明");
        Stream<String> list1=list.stream();
        //将Stream流转换为集合
        List<String> list2=list1.collect(Collectors.toList());
    }

    //将stream流转换为Set集合
    @Test
    public void test2(){
        HashSet<String> set=new HashSet<>();
        Collections.addAll(set,"张杰","张曼玉","张无忌","刘德华","黎明");
        Stream<String> list1=set.stream();
        //将Stream流转换为集合
        Set<String> list2=list1.collect(Collectors.toSet());
    }

    //将stream流转换为Map集合
    @Test
    public void test3(){
        HashMap<Integer,String> map=new HashMap<>();
        Stream<Map.Entry<Integer,String>> streamMap=map.entrySet().stream();
        Map<Integer,String> map1=streamMap.collect(Collectors.toMap((a)->a.getKey(),(a)->a.getValue()));


//        String arrs[]={"张曼玉,30","刘德华,50"};
//        Stream<String> stringStream=Stream.of(arrs);
//        Map<String,Integer> map=
//                stringStream.collect(
//                        Collectors.toMap(
//                                (str)->str.split(",")[0],
//                                (str)->Integer.parseInt(str.split(",")[1])));
    }
}
